package com.easysoft.framework.action;

import com.easysoft.commons.utils.DateTimeUtils;
import com.easysoft.commons.utils.StringUtils;
import com.easysoft.commons.utils.WebConstants;
import com.easysoft.framework.annotation.logs.SystemControllerLog;
import com.easysoft.framework.article.model.SysProblem;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.article.model.SysProblemQuery;
import com.easysoft.framework.article.model.SysProblemView;
import com.easysoft.framework.article.service.ProblemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping(SystemProblemAction.BASE_VIEW)
public class SystemProblemAction {
    public static final String BASE_VIEW = "/problem";
    private final static Logger logger = LoggerFactory.getLogger(SystemProblemAction.class);

    @Autowired
    @Qualifier("service.ProblemService")
    private ProblemService problemService;

    /**
     * 分页查询常见问题列表
     *
     * @param page  分页信息
     * @param query 查询条件
     * @return 常见问题列表
     */
    @RequestMapping(value = "/listProblem", method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView listProblem(Page page, SysProblemQuery query) {
        logger.info("Call SystemProblemAction.listProblem({},{})", page.toString(), query.toString());

        List<SysProblemView> list = problemService.selectSysProblemByPage(page, query);

        ModelAndView mv = new ModelAndView(BASE_VIEW + "/listProblem");
        mv.addObject("page", page);
        mv.addObject("query", query);
        mv.addObject("listProblem", list);
        return mv;
    }

    @RequestMapping(value = "/editProblem", method = RequestMethod.GET)
    public ModelAndView editProblem(String action, @RequestParam(required = false) String id){
        logger.info("Call SystemProblemAction.editProblem({},{})", action, id);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/editProblem");
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(id)) {
                return null;
            }
            return mv;
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(id)) {
                return null;
            }
            SysProblem entity = problemService.selectSysProblemById(id);
            if (entity == null) {
                return null;
            }
            mv.addObject("entity", entity);
            return mv;
        }
        return null;
    }

    @RequestMapping(value = "/saveProblem",method = RequestMethod.POST)
    public int saveProblem(String action,SysProblem problem, HttpSession session){
        logger.info("Call SystemManagerAction.saveManager({},{})", action,problem.toString());
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(problem.getId())) {
                return 0;
            }
            return addProblem(problem,session);
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(problem.getId())) {
                return 0;
            }
            return updateProblem(problem,session);
        }
        return 0;
    }

    /**
     * 添加常见问题
     *
     * @param problem 常见问题表单信息
     * @return 1 成功，0 失败
     */
    @SystemControllerLog(description = "新增常见问题")
    public int addProblem(SysProblem problem, HttpSession session) {
        logger.info("Call SystemProblemAction.addProblem({})", problem.toString());
        if (StringUtils.isNullOrBlank(problem.getCategoryId()) || StringUtils.isNullOrBlank(problem.getContent()) || StringUtils.isNullOrBlank(problem.getTitle())) {
            return 0;
        }
        problem.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));

        problem.setIdentifier(String.valueOf(System.currentTimeMillis()));
        return problemService.addSysProblem(problem);
    }


    /**
     * 更新常见问题信息
     *
     * @param problem 常见问题表单信息
     * @return 1 成功，0 失败
     */
    @SystemControllerLog(description = "更新常见问题")
    public int updateProblem(SysProblem problem, HttpSession session) {
        logger.info("Call SystemProblemAction.updateProblem({})", problem.toString());
        if (StringUtils.isNullOrBlank(problem.getCategoryId()) || StringUtils.isNullOrBlank(problem.getContent()) || StringUtils.isNullOrBlank(problem.getTitle())) {
            return 0;
        }
        problem.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        return problemService.updateSysProblem(problem);
    }

    /**
     * 根据主键删除常见问题
     *
     * @param id 主键
     * @return 1 成功，0 失败
     */
    @SystemControllerLog(description = "删除常见问题")
    @RequestMapping(value = "/deleteProblem", method = RequestMethod.POST)
    public int deleteProblem(String id) {
        logger.info("Call SystemProblemAction.deleteProblem({})", id);
        return problemService.deleteSysProblem(id);
    }

    /**
     * 更改常见问题状态
     *
     * @param id     主键
     * @param status 状态 0 未发布，1 发布
     * @return 1 成功，0 失败
     */
    @SystemControllerLog(description = "更改常见问题状态")
    @RequestMapping(value = "/updaterProblemStatus", method = RequestMethod.POST)
    public int updaterProblemStatus(String id, int status,HttpSession session) {
        logger.info("Call SystemProblemAction.updaterProblemStatus({},{})", id, status);
        SysProblem temp = problemService.selectSysProblemById(id);
        temp.setIsStatis(status);
        temp.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        return problemService.updateSysProblem(temp);
    }
}
